﻿using CodeApi.Context;
using CodeApi.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace CodeApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MoveLetterController : ControllerBase
    {
        public IndexContext Context;

        public MoveLetterController(IndexContext context)
        {
            this.Context = context;
        }

        // GET: api/<MoveLetterController>
        [HttpGet]
        public async Task<ActionResult<IEnumerable<MoveLetterModel>>> Get()
        {
            return await Context.MoveLetter.ToListAsync();
        }


        // POST api/<MoveLetterController>
        [HttpPost]
        public async Task<bool> PostLetter([FromBody]MoveLetterModel model)
        {
            if (model!=null)
            {
                await Context.MoveLetter.AddAsync(model);
                await Context.SaveChangesAsync();
                return await Context.MoveLetter.AnyAsync(t => t.Content == model.Content);
            }
            return false;
        }

        // DELETE api/<MoveLetterController>/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> Delete(int id)
        {
            var letterModel = await Context.MoveLetter.FindAsync(id);
            if (letterModel == null)
            {
                return NotFound();
            }

            Context.MoveLetter.Remove(letterModel);
            await Context.SaveChangesAsync();

            return NoContent();
        }
    }
}
